{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-10-23T16:43:13.709567200Z",
     "start_time": "2023-10-23T16:43:13.318445200Z"
    }
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'dic_list' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[1], line 4\u001B[0m\n\u001B[0;32m      2\u001B[0m dic_list \u001B[38;5;241m=\u001B[39m {\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mname\u001B[39m\u001B[38;5;124m\"\u001B[39m:\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m路飞\u001B[39m\u001B[38;5;124m\"\u001B[39m}\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28;01mdel\u001B[39;00m dic_list\n\u001B[1;32m----> 4\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mdic_list\u001B[49m)\n",
      "\u001B[1;31mNameError\u001B[0m: name 'dic_list' is not defined"
     ]
    }
   ],
   "source": [
    "# 删除字典\n",
    "dic_list = {\"name\":\"路飞\"}\n",
    "del dic_list\n",
    "print(dic_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '索隆', 'age': '22'}\n",
      "{'name': '索隆', 'age': '22'}\n",
      "name_dict  id值:1525549587904\n",
      "new_dict   id值:1525549821504\n",
      "na         id值:1525549821504\n"
     ]
    }
   ],
   "source": [
    "name_dict = {\"name\":\"索隆\",\"age\":\"22\"}\n",
    "new_dict = name_dict.copy()\n",
    "na = new_dict\n",
    "print(name_dict)\n",
    "print(new_dict)\n",
    "print(f\"name_dict  id值:{id(name_dict)}\")\n",
    "print(f\"new_dict   id值:{id(new_dict)}\")\n",
    "print(f\"na         id值:{id(na)}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-23T16:51:57.322138800Z",
     "start_time": "2023-10-23T16:51:57.310178500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "name_dict = {\"name\":\"索隆\",\"age\":\"22\"}\n",
    "dict_len = len(name_dict)\n",
    "print(dict_len)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-23T16:58:57.845515500Z",
     "start_time": "2023-10-23T16:58:57.835115800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "键在字典里存在\n"
     ]
    }
   ],
   "source": [
    "name_dict = {\"name\":\"索隆\",\"age\":\"22\"}\n",
    "key = \"name\"    # 键\n",
    "if key in name_dict:\n",
    "    print(\"键在字典里存在\")\n",
    "else:\n",
    "    print(\"键在字典里不存在\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-23T17:03:32.895728500Z",
     "start_time": "2023-10-23T17:03:32.872801Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名==索隆\n",
      "年龄==22\n",
      "职位==副船长\n"
     ]
    }
   ],
   "source": [
    "dict_items = {\"姓名\":\"索隆\",\"年龄\":\"22\",\"职位\":\"副船长\"}\n",
    "for name,team in dict_items.items():\n",
    "    print(f\"{name}=={team}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-23T17:09:35.192257Z",
     "start_time": "2023-10-23T17:09:35.183287600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名\n",
      "年龄\n",
      "职位\n"
     ]
    }
   ],
   "source": [
    "dict_keys = {\"姓名\":\"索隆\",\"年龄\":\"22\",\"职位\":\"副船长\"}\n",
    "for i in dict_keys.keys():\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T11:27:06.648321800Z",
     "start_time": "2023-10-24T11:27:06.629319300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索隆\n",
      "22\n",
      "副船长\n"
     ]
    }
   ],
   "source": [
    "dict_values = {\"姓名\":\"索隆\",\"年龄\":\"22\",\"职位\":\"副船长\"}\n",
    "for i in dict_values.values():\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T11:27:14.028269800Z",
     "start_time": "2023-10-24T11:27:14.012335500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'语文': 98, '数学': 95, '英语': 90, '生物': 85, '化学': 92, '政治': 90, '历史': 90, '地理': 90, '物理': 90}\n",
      "[('生物', 85), ('英语', 90), ('政治', 90), ('历史', 90), ('地理', 90), ('物理', 90), ('化学', 92), ('数学', 95), ('语文', 98)]\n"
     ]
    }
   ],
   "source": [
    "dict_lesson = {\n",
    "    \"语文\":98,\"数学\":95,\"英语\":90,\"生物\":85,\"化学\":92,\"政治\":90,\"历史\":90,\"地理\":90,\"物理\":90}\n",
    "sorted_lesson = sorted(dict_lesson.items(), key=lambda item: item[1], reverse=False)\n",
    "print(dict_lesson)\n",
    "print(sorted_lesson)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T14:16:01.640554100Z",
     "start_time": "2023-10-24T14:16:01.626982500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': '索隆', 'age': '22'}, {'name': '路飞', 'age': '23'}, {'name': '山治', 'age': '21'}]\n"
     ]
    }
   ],
   "source": [
    "dict_1 = {\"name\":\"索隆\",\"age\":\"22\"}\n",
    "dict_2 = {\"name\":\"路飞\",\"age\":\"23\"}\n",
    "dict_3 = {\"name\":\"山治\",\"age\":\"21\"}\n",
    "dict_list = [dict_1,dict_2,dict_3]\n",
    "print(dict_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T14:30:04.128559500Z",
     "start_time": "2023-10-24T14:30:04.121584300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "水果:\n",
      "\t苹果\n",
      "\t西瓜\n",
      "\t梨子\n",
      "语言:\n",
      "\t汉语\n",
      "\t英语\n",
      "\t日语\n",
      "动物:\n",
      "\t猫\n",
      "\t狗\n",
      "\t鸟\n"
     ]
    }
   ],
   "source": [
    "sports = {\n",
    "    \"水果\":[\"苹果\",\"西瓜\",\"梨子\"],\n",
    "    \"语言\":[\"汉语\",\"英语\",\"日语\"],\n",
    "    \"动物\":[\"猫\",\"狗\",\"鸟\"]\n",
    "}\n",
    "for name,team in sports.items():\n",
    "    print(f\"{name}:\")\n",
    "    for i in team:\n",
    "        print(f\"\\t{i}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T15:06:18.406402700Z",
     "start_time": "2023-10-24T15:06:18.387250300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "语文:\t姓名:索隆\t  成绩:95\t  \n",
      "数学:\t姓名:路飞\t  成绩:90\t  \n",
      "英语:\t姓名:山治\t  成绩:90\t  \n"
     ]
    }
   ],
   "source": [
    "dict_dict = {\n",
    "    \"语文\":{\"姓名\":\"索隆\",\"成绩\":95},\n",
    "    \"数学\":{\"姓名\":\"路飞\",\"成绩\":90},\n",
    "    \"英语\":{\"姓名\":\"山治\",\"成绩\":90},\n",
    "}\n",
    "for name,team in dict_dict.items():\n",
    "    print(f\"{name}:\",end=\"\\t\")\n",
    "    for i,j in team.items():\n",
    "        print(f\"{i}:{j}\",end=\"\\t  \")\n",
    "    print()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T15:16:26.199937600Z",
     "start_time": "2023-10-24T15:16:26.187977600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "dict_a = {\"a\":\"A\",\"b\":\"B\",\"c\":\"C\",\"d\":\"D\",\"e\":\"E\"}\n",
    "number_a = len(dict_a)\n",
    "print(number_a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T15:24:39.596302200Z",
     "start_time": "2023-10-24T15:24:39.589760800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 1, 'c': 1}\n",
      "{'a': None, 'b': None, 'c': None}\n"
     ]
    }
   ],
   "source": [
    "dict_fromkeys = dict.fromkeys([\"a\",\"b\",\"c\"],1)\n",
    "print(dict_fromkeys)\n",
    "# value值为空时\n",
    "dict_fromkeys1 = dict.fromkeys([\"a\",\"b\",\"c\"])\n",
    "print(dict_fromkeys1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T15:31:50.126998300Z",
     "start_time": "2023-10-24T15:31:50.108059700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "路飞\n",
      "未知\n"
     ]
    }
   ],
   "source": [
    "dict_get = {\"姓名\":\"路飞\",\"年龄\":\"22\"}\n",
    "get_value = dict_get.get(\"姓名\")\n",
    "print(get_value)\n",
    "get_None = dict_get.get(\"学校\",\"未知\")\n",
    "print(get_None)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T15:45:33.821481700Z",
     "start_time": "2023-10-24T15:45:33.803237700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "路飞\n",
      "海贼王\n",
      "{'姓名': '路飞', '年龄': '22', '职业': '海贼王'}\n"
     ]
    }
   ],
   "source": [
    "# 当键在字典时：\n",
    "dict_set = {\"姓名\":\"路飞\",\"年龄\":\"22\"}\n",
    "set_value = dict_set.setdefault(\"姓名\")\n",
    "print(set_value)\n",
    "# 键不在字典内时：\n",
    "set_None = dict_set.setdefault(\"职业\",\"海贼王\")\n",
    "print(set_None)\n",
    "# 显示字典中的所有元素\n",
    "print(dict_set)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T15:53:40.805585900Z",
     "start_time": "2023-10-24T15:53:40.783227100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'姓名': '路飞', '年龄': '22', '职业': '海贼王'}\n",
      "{'姓名': '路飞', '年龄': '22'}\n"
     ]
    }
   ],
   "source": [
    "dict_pop = {'姓名': '路飞', '年龄': '22', '职业': '海贼王'}\n",
    "print(dict_pop)\n",
    "dict_pop.pop(\"职业\")\n",
    "print(dict_pop)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-24T15:58:01.335293700Z",
     "start_time": "2023-10-24T15:58:01.324332400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
